package com.example.wan1.view.system.navi;

import com.example.wan1.R;
import com.example.wan1.adapter.NaviAdapter;
import com.example.wan1.base.fragment.SimpleFragment;
import com.example.wan1.model.NaviBean;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.adapter.TabAdapter;
import q.rorbin.verticaltablayout.widget.ITabView;
import q.rorbin.verticaltablayout.widget.QTabView;
import q.rorbin.verticaltablayout.widget.TabView;

import static androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_IDLE;

/***
 * 创建时间：2020/7/8 21:47
 * 创建人：10850
 * 功能描述：
 */
public class NaviFragment extends SimpleFragment implements NaviContract.View {

    @BindView(R.id.nav_vt)
    VerticalTabLayout mNavVt;
    @BindView(R.id.nav_rv)
    RecyclerView mNavRv;

    @Inject
    NaviPresenter mPresenter;
    @Inject
    List<NaviBean> mList;
    @Inject
    NaviAdapter mAdapter;
    @Named("vertical")
    @Inject
    LinearLayoutManager mLayout;

    private TabAdapter mTabAdapter;
    private List<String> mTitles;

    @Override
    protected void initData() {
        mPresenter.getNaviData();
    }

    @Override
    protected void initView() {
        showLoading();
        mTitles = new ArrayList<>();
        //mList = new ArrayList<>();
        //mAdapter = new NaviAdapter(R.layout.navi_rv_item, mList);
        mAdapter.bindToRecyclerView(mNavRv);
        //mLayout = new LinearLayoutManager(mContext);
        mNavRv.setLayoutManager(mLayout);
        //双向联动
        mNavVt.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {
                mLayout.scrollToPositionWithOffset(position, 0);
                //mNavRv.scrollToPosition(position);
                //mLayout.smoothScrollToPosition(mNavRv, new RecyclerView.State(), position);
            }

            @Override
            public void onTabReselected(TabView tab, int position) {

            }
        });
        mNavRv.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState) {
                    case SCROLL_STATE_IDLE:
                        int firstCompletelyVisibleItemPosition = mLayout.findFirstCompletelyVisibleItemPosition();
                        int firstVisibleItemPosition = mLayout.findFirstVisibleItemPosition();

                        if (firstVisibleItemPosition != -1 && firstCompletelyVisibleItemPosition != -1)
                            if (firstCompletelyVisibleItemPosition > firstVisibleItemPosition) {
                                if (mNavVt != null)
                                    mNavVt.setTabSelected(firstVisibleItemPosition);
                                int tabMinus = mTitles.size() - 1 - mNavVt.getSelectedTabPosition();
                                int rvMinus = mLayout.getItemCount() - 1 - firstVisibleItemPosition;
                                if (tabMinus > rvMinus)
                                    mLayout.scrollToPositionWithOffset(firstVisibleItemPosition, 0);

                            } else {
                                mNavVt.setTabSelected(firstVisibleItemPosition);
                            }
                        break;
                }

            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        mNavRv.addOnScrollListener(mOnScrollListener);
    }

    @Override
    protected int initLayoutRes() {
        return R.layout.fragment_navi;
    }

    @Override
    public void onSuccess(List<NaviBean> data) {
        hideLoading();
        //do something
        for (int i = 0; i < data.size(); i++) {
            mTitles.add(data.get(i).getName());
        }
        mTabAdapter = new TabAdapter() {
            @Override
            public int getCount() {
                return mTitles.size();
            }

            @Override
            public ITabView.TabBadge getBadge(int position) {
                return null;
            }

            @Override
            public ITabView.TabIcon getIcon(int position) {
                return null;
            }

            @Override
            public ITabView.TabTitle getTitle(int position) {
                QTabView.TabTitle title = new QTabView.TabTitle.Builder()
                        .setTextColor(R.color.black, R.color.black)
                        .setContent(mTitles.get(position))//设置数据   也有设置字体颜色的方法
                        .build();
                return title;
            }

            @Override
            public int getBackground(int position) {
                return R.drawable.nav_selector;//选中的背景颜色;
            }
        };

        mNavVt.setTabAdapter(mTabAdapter);

        mAdapter.setNewData(data);
    }

    @Override
    public void onFailure(String msg) {
        showError();
    }
}